﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports System.Web.UI.WebControls

Namespace WillStrohl.Modules.CodeCamp

    Partial Public MustInherit Class SpeakerRegistrationSettings
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private p_SpeakerMessage As String = String.Empty
        Private p_CodeCampId As Integer = Null.NullInteger

#End Region

#Region " Private Properties "

        Protected ReadOnly Property SpeakerMessage As String
            Get
                If String.IsNullOrEmpty(Me.p_SpeakerMessage) And Not Me.Settings("SpeakerMessage") Is Nothing Then
                    Me.p_SpeakerMessage = Me.Settings("SpeakerMessage").ToString
                End If

                Return Me.p_SpeakerMessage
            End Get
        End Property

        Private ReadOnly Property CodeCampId As Integer
            Get
                If Me.p_CodeCampId = Null.NullInteger And Not Me.Settings("CodeCampId") Is Nothing Then
                    Integer.TryParse(Me.Settings("CodeCampId").ToString, Me.p_CodeCampId)
                End If

                Return Me.p_CodeCampId
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                ' request that the DNN framework load the jQuery script into the markup
                DotNetNuke.Framework.jQuery.RequestRegistration()

                'Me.RegisterClientScript("jQuery.UI", String.Concat(Me.ControlPath, "js/jquery.ui.min.js"))
                Me.RegisterClientScript("jQuery.UI", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js")

                Me.RegisterCSS("jQuery.UI", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/themes/ui-lightness/jquery-ui.css")

                Me.RegisterClientScript("jQuery.Plugin.TimePicker", String.Concat(Me.ControlPath, "js/jquery.ui.timepicker.js"))
                Me.RegisterCSS("jQuery.UI.Timepicker", String.Concat(Me.ControlPath, "js/jquery.ui.timepicker.css"))

                Me.RegisterClientScript("DNNCodeCampCore", String.Concat(Me.ControlPath, "js/DNNCodeCamp.Core.js"))

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub cvMessage_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvMessage.ServerValidate
            If String.IsNullOrEmpty(Me.txtMessage.Text) Then
                args.IsValid = False
            Else
                args.IsValid = True
            End If
        End Sub

        Private Sub lnkSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSave.Click
            If Page.IsValid Then
                Me.SaveSettings()
            End If
        End Sub

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            Me.LocalizeModule()

            If Not Me.IsPostBack Then

                Dim ctlCodeCamp As New CodeCampController

                ' hide the settings if there are no code camps available to choose from
                Me.pnlNoCodeCamp.Visible = (ctlCodeCamp.CodeCampCount = 0)
                Me.pnlSettings.Visible = (Not Me.pnlNoCodeCamp.Visible)

                If Me.pnlSettings.Visible Then

                    For Each obj As CodeCampInfo In ctlCodeCamp.GetCodeCampsFutureSorted
                        Me.cboCodeCamp.Items.Add(New ListItem(obj.Title, obj.CodeCampId.ToString))
                    Next
                    Me.cboCodeCamp.Items.Insert(0, New ListItem("---"))

                    ' load saved/default settings
                    If Me.CodeCampId > 0 Then
                        For Each item As ListItem In Me.cboCodeCamp.Items
                            If item.Value = Me.CodeCampId.ToString Then
                                item.Selected = True
                                Exit For
                            End If
                        Next
                    End If

                    If Not String.IsNullOrEmpty(Me.SpeakerMessage) Then
                        Me.txtMessage.Text = Me.SpeakerMessage
                    Else
                        Me.txtMessage.Text = Me.GetLocalizedString("txtMessage.Default.Text")
                    End If

                    Me.LoadSettings()

                End If

            End If

        End Sub

        Private Sub LocalizeModule()

            Me.rfvCloseDate.ErrorMessage = Me.GetLocalizedString("rfvCloseDate.ErrorMessage")
            Me.rfvCloseTime.ErrorMessage = Me.GetLocalizedString("rfvCloseTime.ErrorMessage")
            Me.rfvCodeCamp.ErrorMessage = Me.GetLocalizedString("rfvCodeCamp.ErrorMessage")
            Me.rfvOpenDate.ErrorMessage = Me.GetLocalizedString("rfvOpenDate.ErrorMessage")
            Me.rfvOpenTime.ErrorMessage = Me.GetLocalizedString("rfvOpenTime.ErrorMessage")
            Me.cvMessage.ErrorMessage = Me.GetLocalizedString("cvMessage.ErrorMessage")

        End Sub

        Private Sub LoadSettings()

            If Not Me.Settings("CodeCampId") Is Nothing Then
                For Each item As ListItem In Me.cboCodeCamp.Items
                    If item.Value = Me.Settings("CodeCampId").ToString Then
                        item.Selected = True
                        Exit For
                    End If
                Next
            End If

            If Not Me.Settings("OpenDateTime") Is Nothing Then
                Me.txtOpenDate.Text = DateTime.Parse(Me.Settings("OpenDateTime").ToString).ToString("MM/dd/yyyy")
                Me.txtOpenTime.Text = DateTime.Parse(Me.Settings("OpenDateTime").ToString).ToString("hh:mm tt")
            End If

            If Not Me.Settings("CloseDateTime") Is Nothing Then
                Me.txtCloseDate.Text = DateTime.Parse(Me.Settings("CloseDateTime").ToString).ToString("MM/dd/yyyy")
                Me.txtCloseTime.Text = DateTime.Parse(Me.Settings("CloseDateTime").ToString).ToString("hh:mm tt")
            End If

            If Not Me.Settings("Message") Is Nothing Then
                Me.txtMessage.Text = Me.Settings("Message").ToString
            End If

        End Sub

        Private Sub SaveSettings()

            Dim ctlModule As New Entities.Modules.ModuleController
            Dim sec As New PortalSecurity
            
            If Me.cboCodeCamp.SelectedIndex > 0 Then
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, "CodeCampId", sec.InputFilter(Me.cboCodeCamp.SelectedValue, PortalSecurity.FilterFlag.NoMarkup))
            End If

            If Not String.IsNullOrEmpty(Me.txtOpenDate.Text) And Not String.IsNullOrEmpty(Me.txtOpenTime.Text) Then
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, "OpenDateTime", sec.InputFilter(String.Concat(Me.txtOpenDate.Text, " ", Me.txtOpenTime.Text), PortalSecurity.FilterFlag.NoMarkup))
            End If

            If Not String.IsNullOrEmpty(Me.txtCloseDate.Text) And Not String.IsNullOrEmpty(Me.txtCloseTime.Text) Then
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, "CloseDateTime", sec.InputFilter(String.Concat(Me.txtCloseDate.Text, " ", Me.txtCloseTime.Text), PortalSecurity.FilterFlag.NoMarkup))
            End If

            If Not String.IsNullOrEmpty(Me.txtMessage.Text) Then
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, "Message", sec.InputFilter(Me.txtMessage.Text, PortalSecurity.FilterFlag.NoScripting))
            End If

            Entities.Modules.ModuleController.SynchronizeModule(Me.ModuleId)

            Response.Redirect(NavigateURL)

        End Sub

#End Region

        End Class

End Namespace